﻿using DevTeam;
using System;
using System.Data;
namespace ConfExpertEn
{
    public class DataAccess
    {
        static DataManager dm = null;
        static ConfExpertEn.ceMessenger.ceMessengerSoapClient messengerClient = new ceMessenger.ceMessengerSoapClient();

        public static void Init()
        {
            string server = "10.0.0.4";
            ceGate.ceGateSoapClient gateClient = new ceGate.ceGateSoapClient();

            gateClient.Endpoint.Address = new System.ServiceModel.EndpointAddress(string.Format("http://{0}/ceGate/ceGate.asmx", server));
            messengerClient.Endpoint.Address = new System.ServiceModel.EndpointAddress(string.Format("http://{0}/ceMessenger/ceMessenger.asmx", server));

            DataSet dsCredentials = gateClient.GetCredentials(Gate, Gate, server, DatabaseNamePostFix);
            string connectionString = dsCredentials.Tables["ConnectionString"].Rows[0]["ConnectionString"].ToString();

            dm = new DataManager(connectionString);
        }

        public static void UpdateEn(int rollId, int shelfId, decimal en)
        {
            int result = (int)dm.ExecuteScalar("enUpdEn", rollId, shelfId, en);
            if (result == -1)
            {
                string message = "Sistemde olmayan ya da çıkış yapılmış bir topun eni ölçülemez !!!" + " RollId:" + rollId.ToString();
                messengerClient.SendEmail(Gate, Gate, "ConfExpert", "kasimgulcan.karbel@gmail.com", "En Ölçüm Hatası", message, "oaykut@gmail.com,ali.gunes@karbel.com,riza.demir@karbel.com");
                throw new ApplicationException(message);
            }
        }

        public static string GetShelfName(int shelfId)
        {
            DataTable dt = dm.ExecuteDataTableByQuery("select Name from shelf where shelfId = " + shelfId.ToString());
            if (dt.Rows.Count == 1)
                return dt.Rows[0][0].ToString();
            else
                return "";
        }

        public static string DatabaseNamePostFix { get; set; }
        public static string Gate { get; set; }
    }
}